package com.zhuchaofan.zsbback.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhuchaofan.zsbback.entity.Book;
import com.zhuchaofan.zsbback.mapper.BookMapper;
import com.zhuchaofan.zsbback.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@Service
public class BookServiceImpl implements BookService {
    @Autowired
    private BookMapper bookMapper;

//    @Override
//    public PageInfo<Book> findAllBooks(int page,int limit,String name) {
//        PageHelper.startPage(page,limit);
//        List<Book> list = bookMapper.findAll(name);
//        return new PageInfo<>(list);
//    }
@Override
public PageInfo<Book> findAllBooks(int page, int limit, String name) {
    // 如果 name 不为 null 且不为空字符串，在前后添加 % 符号
    if (name != null && !name.isEmpty()) {
        name = "%" + name + "%";
    }
    // 启动分页
    PageHelper.startPage(page, limit);
    // 调用 Mapper 方法查询数据
    List<Book> list = bookMapper.findAll(name);
    // 返回分页信息
    return new PageInfo<>(list);
}
}
